<!-- 定时任务日志 -->
<script setup lang="ts">
import { reactive, ref, watch, onMounted } from "vue"
import * as TaskJobApi from "@/api/taskJob"
import { type TaskJobLogData, type TaskJobLogDetailData } from "@/api/taskJob/types/taskJob"
import { type SelectData } from "@/api/common/types/common"
import { type FormInstance } from "element-plus"
import { Search, Refresh } from "@element-plus/icons-vue"
import { usePagination } from "@/hooks/usePagination"
import { useTableSort } from "@/hooks/useTableSort"

defineOptions({
  // 命名当前组件
  name: "TaskLog"
})

const loading = ref<boolean>(false)
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()
// 排序
const { sortColumn, sortOrder, handleSortChange } = useTableSort()

// 任务 下拉框
const taskSelectOptions = ref<SelectData[]>([])
const fetchTaskSelectOptions = async () => {
  const data = await TaskJobApi.getTaskJobOptionsApi()
  taskSelectOptions.value = data.data
}

//#region 查
const tableData = ref<TaskJobLogData[]>([])
const searchFormRef = ref<FormInstance | null>(null)
const searchData = reactive({
  taskJobId: undefined,
  status: "",
  result: "",
  executor: ""
})

const getTableData = () => {
  loading.value = true
  TaskJobApi.getTaskJobLogDataApi({
    page: paginationData.currentPage,
    size: paginationData.pageSize,
    sortColumn: sortColumn.value,
    sortOrder: sortOrder.value,
    ...searchData
  })
    .then(({ data }) => {
      paginationData.total = data.total
      tableData.value = data.datas
    })
    .catch(() => {
      tableData.value = []
    })
    .finally(() => {
      loading.value = false
    })
}
const handleSearch = () => {
  paginationData.currentPage === 1 ? getTableData() : (paginationData.currentPage = 1)
}
const resetSearch = () => {
  searchFormRef.value?.resetFields()

  handleSearch()
}
//#endregion

//#region 日志详情
const dialogVisible = ref(false)
const logDetailData = ref<TaskJobLogDetailData[]>()

const handleDetail = (row: TaskJobLogData) => {
  if (!row.id) return
  loading.value = true
  TaskJobApi.getTaskJobLogDetailApi(row.id)
    .then(({ data }) => {
      logDetailData.value = data
      dialogVisible.value = true
    })
    .catch(() => {
      logDetailData.value = []
    })
    .finally(() => {
      loading.value = false
    })
}
const closeDetail = () => {
  dialogVisible.value = false
  logDetailData.value = []
}

//#endregion

onMounted(() => {
  fetchTaskSelectOptions()
})

/** 监听分页参数的变化 */
watch(
  [() => paginationData.currentPage, () => paginationData.pageSize, () => sortColumn.value, () => sortOrder.value],
  getTableData,
  { immediate: true }
)
</script>
<template>
  <div class="app-container">
    <el-card v-loading="loading" shadow="never" class="search-wrapper">
      <el-form ref="searchFormRef" :inline="true" :model="searchData" size="small">
        <el-form-item prop="status" label="状态">
          <el-input v-model="searchData.status" placeholder="请输入" />
        </el-form-item>

        <el-form-item prop="result" label="结果">
          <el-input v-model="searchData.result" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="executor" label="执行人">
          <el-input v-model="searchData.executor" placeholder="请输入" />
        </el-form-item>

        <el-form-item prop="taskJobId" label="任务">
          <el-select
            v-model="searchData.taskJobId"
            clearable
            placeholder="请选择任务"
            style="width: 200px"
            :empty-values="[undefined]"
          >
            <el-option v-for="item in taskSelectOptions" :key="item.id" :label="item.text" :value="item.id" />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card v-loading="loading" shadow="never">
      <div class="table-wrapper" size="small">
        <el-table
          :data="tableData"
          max-height="380"
          size="small"
          striped
          :border="true"
          @sort-change="handleSortChange"
        >
          <el-table-column prop="taskJobName" label="任务名称" align="center" width="120" show-overflow-tooltip />
          <el-table-column prop="status" label="状态" align="center" width="80" />
          <el-table-column prop="result" label="结果" align="center" width="80" />
          <el-table-column prop="executor" label="执行人" align="center" width="100" />
          <el-table-column prop="createTime" label="记录时间" align="center" width="120" sortable="custom" />
          <el-table-column
            prop="resultNote"
            label="结果说明"
            header-align="center"
            align="center"
            show-overflow-tooltip
          />
          <el-table-column fixed="right" label="操作" width="240" align="center">
            <template #default="scope">
              <el-button type="primary" text bg size="small" @click="handleDetail(scope.row)">日志明细</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pager-wrapper">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :total="paginationData.total"
          :page-size="paginationData.pageSize"
          :currentPage="paginationData.currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <!-- 日志详情 -->
    <el-dialog v-model="dialogVisible" title="日志详情" @closed="closeDetail" width="60%">
      <el-table :data="logDetailData" border stripe size="small" height="350">
        <el-table-column label="序号" width="50">
          <template #default="scope">
            <span>{{ scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="result" label="结果" align="center" width="80" />
        <el-table-column prop="createTime" label="记录时间" align="center" width="120" />
        <el-table-column prop="resultNote" label="结果说明" header-align="center" align="left" show-overflow-tooltip />
      </el-table>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;

  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}
</style>
